﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Product;
using BrokerBase.Lib.Tool;
using Broker.View.Comm;

namespace Broker.View.ProductView
{
    /// <summary>
    /// AdjustRateForm.xaml 的交互逻辑
    /// </summary>
    public partial class AdjustRateForm
    {
        private IAdjustRate adjRate;

        private IAdjustRate original;

        public AdjustRateForm(IAdjustRate item)
        {
            InitializeComponent();

            original = item;

            this.adjRate = new AdjustRate(item.Owner);

            BaseTools.CopyTo<IAdjustRate>(original, adjRate);

            Loaded += new RoutedEventHandler(AdjustRateForm_Loaded);
            PreviewClosed += new EventHandler<Telerik.Windows.Controls.WindowPreviewClosedEventArgs>(AdjustRateForm_PreviewClosed);

        }



        void AdjustRateForm_PreviewClosed(object sender, Telerik.Windows.Controls.WindowPreviewClosedEventArgs e)
        {
            if (IsCommit)
            {
                if (Verify())
                {
                    BaseTools.CopyTo<IAdjustRate>(adjRate, original);
                    if (CallBack != null)
                        CallBack(original);
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        void AdjustRateForm_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = adjRate;

            cmbInstance.ItemsSource = ProductUtil.GetAdjConverters();

            if (adjRate.Items.Count == 0 && adjRate.ConverterInstance != null)
                SetDefaultItems();

        }

        public bool IsCommit { get; set; }

        public Action<IAdjustRate> CallBack { get; set; }

        private void btnAdjItemEdit_Click(object sender, RoutedEventArgs e)
        {
            IAdjustRateItem item=gvAdj.SelectedItem as IAdjustRateItem;

            AdjustRateItemForm frm = new AdjustRateItemForm(item);

            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            frm.Owner = this;
            frm.ShowDialog();

            gvAdj.ItemsSource = null;
            gvAdj.ItemsSource = adjRate.Items;
        }

        private void SetDefaultItems()
        {
            IList<IAdjustRateItem> items=adjRate.ConverterInstance.GetDefaultItems();
            adjRate.Items = items;
            gvAdj.ItemsSource = adjRate.Items;
        }

        private void btnAdjItemAdd_Click(object sender, RoutedEventArgs e)
        {
            IAdjustRateItem item = new AdjustRateItem(adjRate);

            AdjustRateItemForm frm = new AdjustRateItemForm(item);

            frm.Owner = this;
            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;

            frm.ShowDialog();

            adjRate.Items.Add(item);

            gvAdj.ItemsSource = null;
            gvAdj.ItemsSource = adjRate.Items;
        }

        private void btnAdjItemDel_Click(object sender, RoutedEventArgs e)
        {
            IAdjustRateItem item = gvAdj.SelectedItem as IAdjustRateItem;

            if (item != null)
            {
                if (MessageBox.Show(string.Format("是否要删除调整费率项目:[{0}]",item.Name), "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    adjRate.Items.Remove(item);
                    gvAdj.ItemsSource = null;
                    gvAdj.ItemsSource = adjRate.Items;
                }
            }
        }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            BaseTools.CopyTo<IAdjustRate>(adjRate, original);
            IsCommit = true;
            Close();
        }

        private void cmbInstance_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (adjRate.ConverterInstance != null)
            {
                adjRate.Items = adjRate.ConverterInstance.GetDefaultItems();
                gvAdj.ItemsSource = adjRate.Items;
            }
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            IsCommit = false;
            Close();
        }

        private bool Verify()
        {
            return true;
        }
    }
}
